package org.csu.backofficemanagersys.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.csu.backofficemanagersys.domain.Account;
import org.csu.backofficemanagersys.domain.Admin;
import org.csu.backofficemanagersys.mapper.AccountMapper;
import org.csu.backofficemanagersys.mapper.AdminMapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminService {

    @Autowired
    private AdminMapper adminMapper;

    public List<Admin> selectAll(){

        return adminMapper.selectList(null);
    }

    //TODO 查，模糊查询
    public List<Admin> findByName(String adminname){

        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("adminname",adminname);
        return adminMapper.selectList(queryWrapper);
    }

    public List<Admin> findByPhone(String phone){

        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("phone",phone);
        return adminMapper.selectList(queryWrapper);
    }


    public List<Admin> findByNameAndPhone(String adminname,String phone){

        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("adminname",adminname).like("phone",phone);
        return adminMapper.selectList(queryWrapper);
    }




    public Admin selectOneByNameAndPassword(String admin_name,String password){

        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("adminname",admin_name).eq("password",password);
        return adminMapper.selectOne(queryWrapper);
    }
    public Admin selectByPhone(String phone){
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        return adminMapper.selectOne(queryWrapper);
    }

    //TODO 通过账号密码


    //TODO 增
    public Boolean addAdmin(Admin admin){
        try {
            adminMapper.insert(admin);
            return true;
        }catch (Exception e){
            return false;
        }

    }

    //TODO 删by Id
    public Boolean deleteAdminById(int id){
        return adminMapper.deleteById(id)!=0;
    }

    //TODO 改 通过名字找到此人，只能修改密码和手机号，需要MD5加密
    public Boolean updateAdmin(String admin_name,String password,String phone){
        UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("adminname",admin_name).set("password",password).set("phone",phone);
        return adminMapper.update(null,updateWrapper)!=0;
    }

}
